#!/bin/sh


# tee output to testdetails.log
if ( test "$CHILD" != "1" )
then
	CHILD=1 $0 2>&1 | tee testdetails.log &
	PID=$!
	wait $PID
	exit $?
fi



echo "SQL Relay test results:" > testresults.log
echo >> testresults.log

PREFIX=@prefix@

# stop any existing instances
$PREFIX/bin/sqlr-stop
sleep 2

# really kill them
ps aux > /dev/null 2> /dev/null
if ( test "$?" = "0" )
then
	kill `ps aux | grep sqlr-connection | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps aux | grep sqlr-scaler | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps aux | grep sqlr-listener | grep -v grep | cut -c10-15` 2> /dev/null
else
	kill `ps -efa | grep sqlr-connection | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps -efa | grep sqlr-scaler | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps -efa | grep sqlr-listener | grep -v grep | cut -c10-15` 2> /dev/null
fi
sleep 2

# for each database/configuration...
for DB in @TESTDBS@
do

	# make sure that support for the appropriate database exists
	MODULE=$DB
	case "$DB" in
		tls|krb|extensions)
			MODULE=oracle
			;;
		mysql*)
			MODULE=mysql
			;;
		postgresql*)
			MODULE=postgresql
			;;
	esac
	if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_$MODULE.* 2> /dev/null`" )
	then
		echo "skipping $DB..."
		echo
		echo "================================================================================"
		echo
		continue
	fi

	# for router tests, also verify that we support mysql
	if ( test "$DB" = "router" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_mysql.* 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# for mssql tests, also verify that we have an odbc config for it
	if ( test "$DB" = "mssql" )
	then
		if ( test -z "`grep mssqlodbc /etc/odbc.ini 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# testing...
	echo "testing $DB (from `hostname`)..."
	echo "$DB..." >> testresults.log

	# for the router test, start the master/slave instances
	if ( test "$DB" = "router" )
	then
		$PREFIX/bin/sqlr-start -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id routermaster -backtrace @abs_top_builddir@/test
		sleep 2

		$PREFIX/bin/sqlr-start -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id routerslave -backtrace @abs_top_builddir@/test
		sleep 2
	fi

	# start the instance
	$PREFIX/bin/sqlr-start -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id ${DB}test -backtrace @abs_top_builddir@/test
	sleep 2

	# make sure that the instance is up
	PING=""
	BYPASS="no"
	for attempts in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
	do
		echo
		echo "pinging $DB..."
		if ( test "$DB" = "tls" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -tls -tlscert @abs_top_builddir@/test/sqlrelay.conf.d/tls/client.pem -tlsca @abs_top_builddir@/test/sqlrelay.conf.d/tls/ca.pem -tlsvalidate ca -command ping 2>&1`
		elif ( test "$DB" = "extensions" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -user test -password test -command ping 2>&1`
		elif ( test "$DB" = "mysqlprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 3306`" )
			then
				PING="The database is up."
			fi
		elif ( test "$DB" = "postgresqlprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 5432`" )
			then
				PING="The database is up."
			fi
		elif ( test "$DB" = "tdsprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 1433`" )
			then
				PING="The database is up."
			fi
		elif ( test "$DB" = "oracleprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 1521`" )
			then
				PING="The database is up."
			fi
		elif ( test "$DB" = "teradataprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 1025`" )
			then
				PING="The database is up."
			fi
		else
			PING=`$PREFIX/bin/sqlrsh -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id ${DB}test -command ping 2>&1`
		fi

		# (this collapses whitespace)
		PING=`echo $PING`

		echo $PING

		if ( test "$PING" = "0: Couldn't connect to the listener." )
		then
			sleep 5
		else

			# If ssl is sufficiently old then the ssl test will
			# fail like this.  Just bypass the test in that case.
			SSLPROBLEM=`echo "$PING" | grep "unknown message digest algorithm"`
			if ( test -n "$SSLPROBLEM" )
			then
				BYPASS="yes"
			fi
			break
		fi
	done

	# bypass the test if necessary
	if ( test "$BYPASS" = "no" )
	then

		# run the tests
		if ( test "$PING" = "The database is up." )
		then

			# kinit for krb tests
			if ( test "$DB" = "krb" )
			then
				kdestroy
				kinit
			fi

			echo
			echo "success..."
			echo

			if ( test "$DB" = "mysqlprotocol" )
			then
				cd protocol/mysql
				./mysql
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "protocol" >> ../../testresults.log
				else
					printf "    % 8s: failed\n" "protocol" >> ../../testresults.log
				fi
				cd ../..
			elif ( test "$DB" = "postgresqlprotocol" )
			then
				cd protocol/postgresql
				./postgresql
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "protocol" >> ../../testresults.log
				else
					printf "    % 8s: failed\n" "protocol" >> ../../testresults.log
				fi
				cd ../..
			elif ( test "$DB" = "tdsprotocol" )
			then
				cd protocol/tds
				./tds
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "protocol" >> ../../testresults.log
				else
					printf "    % 8s: failed\n" "protocol" >> ../../testresults.log
				fi
				cd ../..
			elif ( test "$DB" = "oracleprotocol" )
			then
				cd protocol/oracle
				./oracle
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "protocol" >> ../../testresults.log
				else
					printf "    % 8s: failed\n" "protocol" >> ../../testresults.log
				fi
				cd ../..
			elif ( test "$DB" = "teradataprotocol" )
			then
				cd protocol/teradata
				./teradata
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "protocol" >> ../../testresults.log
				else
					printf "    % 8s: failed\n" "protocol" >> ../../testresults.log
				fi
				cd ../..
			else
				./test.sh $DB
			fi
		else
			echo
			echo "failed to start ${DB}test"
			echo
			echo "     connect: failed" >> testresults.log
		fi
	fi

	# shut down the instance(s)
	if ( test "$DB" = "router" )
	then
		sleep 2
		$PREFIX/bin/sqlr-stop -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id routermaster
		sleep 2
		$PREFIX/bin/sqlr-stop -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id routerslave
	fi
	sleep 2
	$PREFIX/bin/sqlr-stop -config @abs_top_builddir@/test/sqlrelay.conf.d/${DB}.conf -id ${DB}test
	sleep 2

	rm -f log/*.log

	echo
	echo "================================================================================"
	echo
done

cat testresults.log

exit 0
